/*
	
*/

var adminLoginedInfo = Cookies.getJSON('kplus_ad_logined_info');
var SYS_TEM_MAIL_ID = ['kpMail201803280101', 'kpMail201803280112', 'kpMail201803280113', 'kpMail201803280114', 'kpMail201803280115']

function DomInit(){

	var ajaxData = { aId: adminLoginedInfo.aId, aToken: adminLoginedInfo.aToken};
	var w_id = getParam('model_id');
	// wang editor 富文本编辑器
	var WEditor = window.wangEditor, editorHtml = new WEditor('#html');
	editorHtml.create();

	var initForm = function(data, form){
		var form_data = {
    		model_id: data.model_id,
    		name: data.name,
    		title: data.subject,
    	}
        //富文本载入
    	editorHtml.txt.html(data.html);
    	form.val('email-detail-from', form_data);
    	form.render();
	} 

	this.getMailDetail = function(layer, form){

		var loadIndex = layer.load(1, { shade: [0.5, '#000'] });
		var _ajaxData = $.extend(ajaxData, {'wId': w_id})

		$.ajax({
			url: BASE_SERVICEURL + URlS.sys_email,
			type: 'GET',
			data: _ajaxData,
			success: function(responseData){
				layer.close(loadIndex);
				if(responseData.msg.code == 200){
					//console.log(responseData.data)
					initForm(responseData.data, form);
				}else{
					layer.msg(responseData.msg.msg, {time: 2000})
				}
			},
			error: function(responseData){
				layer.close(loadIndex);
				layer.msg(MESSAGES.service_error, {time: 2000});
			}
		});
	}

	this.modifyMailInfo = function(newData){
		var loadIndex = layer.load(1, { shade: [0.5, '#000'] });
		var _ajaxData = $.extend(ajaxData, newData, {html: editorHtml.txt.html()});

		$.ajax({
			url: BASE_SERVICEURL + URlS.sys_email,
			type: 'DELETE',
			data: _ajaxData,
			success: function(responseData){
				layer.close(loadIndex);
				if(responseData.msg.code == 200){
					//console.log(responseData.data)
					layer.msg(responseData.data.msg, {time: 2000})
				}else{
					layer.msg(responseData.msg.msg, {time: 2000})
				}
			},
			error: function(responseData){
				layer.close(loadIndex);
				layer.msg(MESSAGES.service_error, {time: 2000});
			}
		});
	}

	this.deleteMailModel = function(){

		var loadIndex = layer.load(1, { shade: [0.5, '#000'] });
		var data = {
			wId: w_id,
			sign: 'delete'
		}
		var _ajaxData = $.extend(ajaxData, data);

		$.ajax({
			url: BASE_SERVICEURL + URlS.sys_email,
			type: 'DELETE',
			data: _ajaxData,
			success: function(responseData){
				layer.close(loadIndex);
				if(responseData.msg.code == 200){
					//console.log(responseData.data)
					layer.msg(responseData.data.msg, {time: 2000, end: function(){
						//关闭窗口并刷新父窗口
                    	parent.layer.close(layer.index);
                    	window.parent.location.reload();
					}});
				}else{
					layer.msg(responseData.msg.msg, {time: 2000})
				}
			},
			error: function(responseData){
				layer.close(loadIndex);
				layer.msg(MESSAGES.service_error, {time: 2000});
			}
		});


	}
}

$(function(){
	layui.use(['layer', 'form'], function(){
		var layer = layui.layer, form = layui.form;
		
		var dom_init = new DomInit();
		dom_init.getMailDetail(layer, form);

		//修改
		form.on('submit(modify)', function(data){
        	//console.log(data)
        	var form_data = {
        		name: data.field.name,
                title: data.field.title,
                admin: adminLoginedInfo.aId
        	};
        	
        	layer.confirm("確認修改郵件範本？", {title:'提示'}, function(index){
				dom_init.modifyMailInfo(form_data);
				layer.close(index); //关闭询问框
			});
        	return false;
        })
        //删除
        form.on('submit(delete)', function(data){

        	layer.confirm("確認删除郵件範本？", {title:'提示'}, function(index){
        		if($.inArray(getParam('model_id'), SYS_TEM_MAIL_ID) >= 0){
        			layer.msg("系統功能性郵件，無法删除！", {time: 2000});
        			return false;
        		}
				dom_init.deleteMailModel();
				layer.close(index); //关闭询问框
			});
        	return false;
        })
	})
})
























